/**
 * 
 */
package com.xb.cloud.product.merchant.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.redis.CacheUtil;
import com.xb.cloud.common.util.StringUtils;
import com.xb.cloud.product.merchant.service.client.ProductMongoService;
import com.xb.cloud.product.merchant.service.client.ProductSkuDetailService;
import com.xb.cloud.product.merchant.service.client.bo.ProductBo;
import com.xb.cloud.product.merchant.service.client.bo.ProductMongoBo;
import com.xb.cloud.product.merchant.service.client.bo.ProductShopSeriesMongoBo;
import com.xb.cloud.product.service.business.model.ProductSku;
import com.xb.cloud.product.service.business.model.ProductSkuExample;
import com.xb.cloud.product.service.business.model.ProductSkuExtDescription;
import com.xb.cloud.product.service.business.model.ProductSkuExtDescriptionExample;
import com.xb.cloud.product.service.business.model.ProductSkuExtPic;
import com.xb.cloud.product.service.business.model.ProductSkuExtPicExample;
import com.xb.cloud.product.service.business.model.mapper.ProductSkuExtDescriptionMapper;
import com.xb.cloud.product.service.business.model.mapper.ProductSkuExtPicMapper;
import com.xb.cloud.product.service.business.model.mapper.ProductSkuMapper;
import com.xb.cloud.product.service.business.model.mapper.SkuPicMapper;
import com.xb.cloud.product.service.client.BaseProductConstant;
import com.xb.cloud.product.service.client.ProductService;
import com.xb.cloud.product.service.client.bo.ProductDescriptionBo;
import com.xb.cloud.product.service.client.bo.SkuDetailBO;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

/**
 * 商品详情(参考buy)
 * @author kexue
 *
 */
@Service
public class ProductSkuDetailServiceImpl extends AbstractService implements  ProductSkuDetailService{

	@Resource
	SkuPicMapper skuPicMapper;
	 
	@Resource
	ProductSkuMapper productSkuMapper;
	 
	@Autowired
	private ProductMongoService productMongoService;
	@Autowired
	ProductService productService;
	@Resource
	ProductSkuExtDescriptionMapper productSkuExtDescriptionMapper;
	@Resource
	ProductSkuExtPicMapper productSkuExtPicMapper;
	
	 
	@Override
	public ServiceReturnInfo getSKUDetail(Long skuID) {
		
		//首先从redis中获取
		String skuDetailInfo = CacheUtil.get(BaseProductConstant.SKU_DETAIL_REDIS_KEY+"_"+skuID);
		if(!StringUtils.isEmpty(skuDetailInfo)) {
			return ServiceReturnUtils.success(skuDetailInfo);
		}
		
		ProductSkuExample example = new ProductSkuExample();
		
		com.xb.cloud.product.service.business.model.ProductSkuExample.Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(skuID);
		criteria.andIsEnabledEqualTo(DbConstants.PRODUCT_SKU_ENABLED);
		
		List<ProductSku> skuList = productSkuMapper.selectByExample(example);
		
		if(skuList.isEmpty()) {
			return ServiceReturnUtils.error(ServiceReturnCodes.SKU_NOT_EXIST_EXCEPTION);
		}
		ProductSku skuBO = skuList.get(0);
		
		//================开始创建ServiceReturnInfo=====================
		SkuDetailBO skuDetailBO = new SkuDetailBO();
		//设置sku详情主标题与副标题
		skuDetailBO.setName(skuBO.getSkuNameCn());
		skuDetailBO.setSubTitle(skuBO.getSkuTitleSub());
		skuDetailBO.setIsCanSale(skuBO.getIsCanSale());
		
		//获取product信息
		ServiceReturnInfo  productReturnInfo = productService.getProductByID(skuBO.getProductId(), 1);
		if(ServiceReturnUtils.isError(productReturnInfo) || productReturnInfo.getData() == null) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PRODUCT_NOT_EXIST_EXCEPTION);
		}
		ProductBo  productBo = (ProductBo) productReturnInfo.getData();
		//设置sku的规格
		skuDetailBO.setUnit(skuBO.getSkuUnit());
		
		//设置sku详情轮播图片
		List<String> picList = productService.getProudctPicList(productBo.getId());
		skuDetailBO.setPictureURLList(picList);
		
		//获取product图文详细
		
		ServiceReturnInfo detailReturnInfo = productService.getProductDetailByID(skuBO.getProductId());
		
		if(ServiceReturnUtils.isSuccess(detailReturnInfo)) {
			ProductDescriptionBo descriptionBo = (ProductDescriptionBo) detailReturnInfo.getData();
			skuDetailBO.setProductDescription(descriptionBo.getDetailContent());
		}
		
		//获取商家发布sku的文字描述
		ProductSkuExtDescriptionExample descriptionExample = new ProductSkuExtDescriptionExample();
		com.xb.cloud.product.service.business.model.ProductSkuExtDescriptionExample.Criteria detailCriteria = descriptionExample.createCriteria();
		detailCriteria.andSkuIdEqualTo(skuID);
		List<ProductSkuExtDescription> skuExtendDetailList = productSkuExtDescriptionMapper.selectByExampleWithBLOBs(descriptionExample);
		if(!skuExtendDetailList.isEmpty()) {
			skuDetailBO.setSkuExtendDetail(skuExtendDetailList.get(0).getDetailContent());
		}
		
		//获取商家发布sku的上传图片描述
		
		ProductSkuExtPicExample extPicExample = new ProductSkuExtPicExample();
		com.xb.cloud.product.service.business.model.ProductSkuExtPicExample.Criteria extPicCriteria = extPicExample.createCriteria();
		extPicCriteria.andSkuIdEqualTo(skuID);
		List<ProductSkuExtPic> extPicDtoList = productSkuExtPicMapper.selectByExample(extPicExample);
		List<String> extPicList = new ArrayList<String>();
		for (ProductSkuExtPic productSkuExtPic : extPicDtoList) {
			extPicList.add(productSkuExtPic.getPicUrl());
		}
		skuDetailBO.setSkuExtendPicList(extPicList);
		//从mongo获取该sku对应product对应基本属性，如果是系列品包括系列属性
		long productKeyInMongo = productBo.getId();
		/**
		 * 1:如果是单品：mongo中存放的就是productid
		 * 2：如果是系列品，mongo中的key为系列品的主品（需主品）id，所以如果是系列子品（正常请求过来的都是子品），需要获取主品id
		 */
		Byte productType = productBo.getProductType();
		switch (productType) {
			case DbConstants.PRODUCT_TYPE_SINGLE:
				 break;
			case DbConstants.PRODUCT_TYPE_SERIAS_VIRTUAL:
				 break;
			case DbConstants.PRODUCT_TYPE_SERIAS_SUB:
				productKeyInMongo = productService.getMianProductIdBySubProjectId(productKeyInMongo);
			case DbConstants.PRODUCT_TYPE_COMBINATION:
				 break;
		
		}
		
		ServiceReturnInfo productMongoInfo = productMongoService.findById(productKeyInMongo);
		if(ServiceReturnUtils.isError(productMongoInfo) || productMongoInfo.getData() == null) {
			return ServiceReturnUtils.error(ServiceReturnCodes.SKU_MONGO_NOT_EXIST_EXCEPTION);
		}
		skuDetailBO.setSkuID(skuID);
		
		ProductMongoBo productMongoBO = (ProductMongoBo)productMongoInfo.getData();
		//标识该sku是系列品还是单品
		skuDetailBO.setSkuType(productMongoBO.getProductType());
		//设置基本属性
		skuDetailBO.setBaseAttrs(productMongoBO.getBaseAttrs());
		//设置系列属性
		skuDetailBO.setSeriesAttrs(productMongoBO.getSeriesAttrs());
		
		
		if(productMongoBO.getProductType() == DbConstants.PRODUCT_TYPE_SERIAS_VIRTUAL) {
			String skuMongoKey = productKeyInMongo + DbConstants.KEY_SEGMENTATION + skuBO.getShopId(); 
			ServiceReturnInfo skuMongoInfo = productMongoService.findByProductShopId(skuMongoKey);
			if(ServiceReturnUtils.isError(skuMongoInfo) || skuMongoInfo.getData() == null) {
				return ServiceReturnUtils.error(ServiceReturnCodes.SKU_MONGO_NOT_EXIST_EXCEPTION);
				
			}
			ProductShopSeriesMongoBo skuMongoBO = (ProductShopSeriesMongoBo) skuMongoInfo.getData();
			skuDetailBO.setSeriesAttributeCombSkuIdMap(skuMongoBO.getSeriesAttributeCombSkuIdMap());
		}
		
		
		
		//================结束创建ServiceReturnInfo=====================
		
		return ServiceReturnUtils.success(skuDetailBO);
	}
	 
}
